import copy
import re
from docx.shared import RGBColor
from docx import Document

doc = Document(r'E:\BaiduNetdiskWorkspace\1701864\123同步文件夹\确有专长笔记\中药学\《中药学》笔记.docx')


def highlight_word(document, word_to_highlight):
    for paragraph in document.paragraphs:
        for run in paragraph.runs:
            if word_to_highlight in run.text:
                new_text = run.text.replace(word_to_highlight, '')
                print(new_text, word_to_highlight)
                run.bold = True
                run.font.color.rgb = RGBColor(255, 0, 0)
                run.text = new_text + word_to_highlight





# 打印所有段落的文本
list_ = []
num = 1  # 四级标题顺序
# 主药标题级别
masterStyle = "Heading 3"

isDelXianDai = False  # 是否删除 【现代研究】
isDelFuYao = False  # 是否删除附药
isYaoWuJieShao = False  # 是否是药物介绍
isDelKongGe = False  # 是否是空格
beforePara = None
for para in doc.paragraphs:
    # print(para.style.name, para.text)

    # 附药标题级别整理
    if para.style.name.startswith("Normal") and beforePara.text.startswith('第'):
        print(para.text)
        masterStyle = para.style.name
        print(masterStyle)
    # if para.text.startswith('附药：'):
    #     # print(para.style.name, para.text)
    #     if masterStyle == para.style.name:
    #         num = int(re.search(r" (\d)", masterStyle).group(1)) + 1
    #         para.style = doc.styles[f"Heading {num}"]
    #         print(para.style.name, para.text)
    #
    # #  刪除附药
    # if para.text.startswith('附药：'):
    #     isDelFuYao = True
    #
    # if not para.text.startswith('附药：') and para.style.name.startswith("Heading"):
    #     isDelFuYao = False
    #
    # if isDelFuYao:
    #     para._p.getparent().remove(para._p)
    #     continue
    #
    #
    # # 刪除药物来源 ： Qiāndān（《神农本草经》）
    # if para.style.name == 'Normal':
    #     # if "》）" in para.text:
    #     if para.text.endswith("》）"):
    #         # print(para.text)
    #         para._p.getparent().remove(para._p)
    #         continue
    #
    # # 刪除药物介绍 ： 本品为唇形科植物薄荷
    # if para.text.startswith('本品为'):
    #     isYaoWuJieShao = True
    #
    # if para.text.startswith('【药性】'):
    #     isYaoWuJieShao = False
    #
    # if isYaoWuJieShao:
    #     para._p.getparent().remove(para._p)
    #     continue
    #
    # if para.text.startswith('【药性】'):
    #     beforeParaStyle = beforePara.style
    #     if beforePara.style.name.startswith('Heading'):
    #         for run in beforePara.runs:
    #             run.text = run.text.replace(' ', '')
    # # 去除 【现代研究】
    # if para.text.startswith('【现代研究】'):
    #     isDelXianDai = True
    #
    # if para.style.name.startswith('Heading'):
    #     isDelXianDai = False
    #
    # if isDelXianDai:
    #     para._p.getparent().remove(para._p)
    #     continue



    # if para.text.startswith('【功效】'):
    #     print(para.text, )
        # para_run = copy.deepcopy(para.runs[0])
        #
        # para_run.text = "【功效】"
        # doc.insertParagraph(para_run)
        # doc.paragraphs.append(para_run)
        # para.runs[0].text = para.runs[0].text.lstrip('【功效】')
        # print(para.runs[0].text.lstrip('【功效】'))

        # highlight_word(doc, para.runs[0].text.lstrip('【功效】').strip())
        # # 设置加粗
        # para.runs[0].bold = True
        # # 设置颜色为红色（可根据需求调整颜色值）
        # para.runs[0].font.color.rgb = RGBColor(255, 0, 0)
        # break
        # para.runs[0].text = f"【功效】<b><font color=red>{'23'}</font></b>"
        # # para.runs.insert(0, para_run)
        # for run in para.runs:
        #     print(run.text)
        #     words = run.text.split()
        #     new_run_text = ""
        #     for word in words:
        #         if target_word == word:
        #             bold_word = f"<b><font color=red>{word}</font></b>"
        #             new_run_text += bold_word
        #         else:
        #             new_run_text += word + " "
        #     run.text = new_run_text.strip()
        # break

    beforePara = para




# doc.save('《中药学》笔记00.docx')